<template>
    <view class="jnpf-wrap jnpf-wrap-form" v-if="!loading">
        <u-form :model="dataForm" :rules="rules" ref="dataForm" :errorType="['toast']"
                label-position="left" label-align="right" :label-width="labelwidth" class="jnpf-form">
			<u-form-item  
 label="姓名"  prop="username" >
				<u-input v-model="dataForm.username"
 placeholder="请输入" :style='{"width":"100%"}' disabled="disabled">
				</u-input>
			</u-form-item>
			<u-form-item  
 label="关联账号"  prop="userid" >
				<jnpf-user-select v-model="dataForm.userid"
 placeholder="请选择" disabled="disabled">
				</jnpf-user-select>
			</u-form-item>
			<u-form-item  
 label="公司名称"  prop="companyname" >
				<jnpf-com-select v-model="dataForm.companyname"
 placeholder="请选择" type="organize" disabled="disabled">
				</jnpf-com-select>
			</u-form-item>
			<u-form-item  
 label="性别"  prop="gender" >
				<u-radio-group v-model="dataForm.gender" >
					<u-radio v-for="(item, index) in genderOptions" :key="index" :name="item.id"  disabled="disabled">
						{{item.fullName}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item  
 label="档案寸照"  prop="lnchimage" >
				<jnpf-upload v-model="dataForm.lnchimage"
 sizeUnit="MB"  :fileSize="5"  :limit="1"  :value="dataForm.lnchimage">
				</jnpf-upload>
			</u-form-item>
			<u-form-item  
 label="职称"  prop="companytitle" >
				<u-input v-model="dataForm.companytitle"
 placeholder="请输入" :style='{"width":"100%"}' disabled="disabled">
				</u-input>
			</u-form-item>
			<u-form-item  
 label="建档时间"  prop="creatortime" >
				<jnpf-date-time v-model="dataForm.creatortime"
 placeholder="请选择" type="date" :style='{"width":"100%"}' disabled="disabled">
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="健康状况"  prop="health" disabled="disabled">
				{{dataForm.health==1?'良好':'较差'}}
			</u-form-item>
			<u-form-item  
 label="身份证号"  prop="cardid" >
				<u-input v-model="dataForm.cardid"
 placeholder="请输入" :style='{"width":"100%"}' disabled="disabled">
				</u-input>
			</u-form-item>
			<u-form-item  
 label="学历"  prop="education" >
				<jnpf-select v-model="dataForm.education"
 placeholder="请选择"  :props="educationProps" :options="educationOptions" :style='{"width":"100%"}' disabled="disabled">
				</jnpf-select>
			</u-form-item>
			<u-form-item  
 label="籍贯"  prop="nativeplace" >
				<u-input v-model="dataForm.nativeplace"
 placeholder="请输入" :style='{"width":"100%"}' disabled="disabled">
				</u-input>
			</u-form-item>
			<u-form-item  
 label="入厂时间"  prop="jointime" >
				<jnpf-date-time v-model="dataForm.jointime"
 placeholder="请选择" type="date" :style='{"width":"100%"}' disabled="disabled">
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="从事工种"  prop="typework" >
				<u-input v-model="dataForm.typework"
 placeholder="请输入" :style='{"width":"100%"}' disabled="disabled">
				</u-input>
			</u-form-item>
			<u-form-item  
 label="家庭住址"  prop="homeaddress" >
				<u-input v-model="dataForm.homeaddress"
 placeholder="请输入" type="textarea" :style='{"width":"100%"}' disabled="disabled">
				</u-input>
			</u-form-item>
            <view class="jnpf-table" >
			<view class="jnpf-table-item" v-for="(item,i) in dataForm.mt_safe_archives_detailList" :key="i">
			<view class="jnpf-table-item-title u-flex u-row-between">
			<text class="jnpf-table-item-title-num">培训记录({{i+1}})</text>
		
			</view>
			<u-form-item 
 label="会议名称" >
				<jnpf-relation-select v-model="dataForm.mt_safe_archives_detailList[i].mettingname"
 relationField='F_MeetingName'  :vModel="'mettingname'+i"   :columnOptions="mettingnameColumnOptions" modelId="350559861072724485"  :pageSize="20"  placeholder="请选择" type="relation" disabled="disabled">
				</jnpf-relation-select>
			</u-form-item>
			<u-form-item 
 label="培训时间" >
				<jnpf-date-time v-model="dataForm.mt_safe_archives_detailList[i].creatortime"
 placeholder="请选择" type="date" :style='{"width":"100%"}' disabled="disabled">
				</jnpf-date-time>
			</u-form-item>
			<u-form-item 
 label="考核成绩" >
				<u-input v-model="dataForm.mt_safe_archives_detailList[i].score"
 placeholder="请输入" :style='{"width":"100%"}' disabled="disabled">
				</u-input>
			</u-form-item>
			</view>
			
            </view>
            <view class="jnpf-table" >
			<view class="jnpf-table-item" v-for="(item,i) in dataForm.mt_safe_7learnjinduList" :key="i">
			<view class="jnpf-table-item-title u-flex u-row-between">
			<text class="jnpf-table-item-title-num">学习记录({{i+1}})</text>
			</view>
			<u-form-item 
 label="培训课程" >
				<jnpf-relation-select v-model="dataForm.mt_safe_7learnjinduList[i].classid"
 relationField='F_Classtitle'  :vModel="'classid'+i"   :columnOptions="classidColumnOptions" modelId="353506502767543429"  :pageSize="20"  placeholder="请选择" type="relation" disabled="disabled">
				</jnpf-relation-select>
			</u-form-item>
			<u-form-item 
 label="培训视频" >
				<jnpf-relation-select v-model="dataForm.mt_safe_7learnjinduList[i].videodetailid"
 relationField='F_Tile'  :vModel="'videodetailid'+i"   :columnOptions="videodetailidColumnOptions" modelId="353615230296655109"  :pageSize="20"  placeholder="请选择" type="relation" disabled="disabled">
				</jnpf-relation-select>
			</u-form-item>
			<u-form-item 
 label="视频进度" >
				<u-input v-model="dataForm.mt_safe_7learnjinduList[i].pace"
 placeholder="请输入" :style='{"width":"100%"}' disabled="disabled">
				</u-input>
			</u-form-item>
			</view>

            </view>
            <view class="jnpf-table" >
			<view class="jnpf-table-item" v-for="(item,i) in dataForm.mt_safe_answersheetList" :key="i">
			<view class="jnpf-table-item-title u-flex u-row-between">
			<text class="jnpf-table-item-title-num">考试记录({{i+1}})</text>
		
			</view>
			<u-form-item 
 label="考试名称" >
				<jnpf-relation-select v-model="dataForm.mt_safe_answersheetList[i].testpaper"
 relationField='F_TestPaperName'  :vModel="'testpaper'+i"   :columnOptions="testpaperColumnOptions" modelId="353864280459707653"  :pageSize="20"  placeholder="请选择" type="relation" disabled="disabled">
				</jnpf-relation-select>
			</u-form-item>
			<u-form-item 
 label="创建时间" >
				<jnpf-date-time v-model="dataForm.mt_safe_answersheetList[i].creatortime"
 placeholder="请选择" type="date" :style='{"width":"100%"}' disabled="disabled">
				</jnpf-date-time>
			</u-form-item>
			<u-form-item 
 label="成绩" >
				<u-input v-model="dataForm.mt_safe_answersheetList[i].score"
 placeholder="请输入" :style='{"width":"100%"}' disabled="disabled">
				</u-input>
			</u-form-item>
			</view>
		
            </view>
        </u-form>
        <view class="buttom-actions" v-if="jurisdictionType != 'btn_detail'" >
            <u-button class="buttom-btn" @click="resetForm">取消</u-button>
            <u-button class="buttom-btn" type="primary" @click="submitForm" :loading="btnLoading">保存</u-button>
        </view>
    </view>
</template>

<script>
    import {
        getDictionaryDataSelector,
		getDataInterfaceRes
    } from '@/api/common'
    import request from '@/utils/request'
    export default {
        data(){
            return{
                btnLoading: false,
				loading: false,
                text: '提示：测试文本',
				dataForm:{
					id:"",
					username : '',
					userid : '',
					companyname : [],
					gender : "1",
					lnchimage : [],
					companytitle : '',
					creatortime : '',
					health : 1,
					cardid : '',
					education : "",
					nativeplace : '',
					jointime : '',
					typework : '',
					homeaddress : '',
					mt_safe_archives_detailList:[],
					mt_safe_7learnjinduList:[],
					mt_safe_answersheetList:[],
				},
				rules:{
				},
                genderOptions:[{"fullName":"男","id":"1"},{"fullName":"女","id":"2"}],
				genderProps:{"label":"fullName","value":"id"},
				educationOptions:[],
				educationProps:{"label":"fullName","value":"id"},
				mettingnameColumnOptions:[
				],
				classidColumnOptions:[
				],
				videodetailidColumnOptions:[
				],
				testpaperColumnOptions:[
				],
                labelwidth:100*1.5,
                menuId:'',
				jurisdictionType:'',
                ruleList:{
                },
            }
        },
        onLoad(option) {
			this.jurisdictionType = option.jurisdictionType
        	this.menuId=option.menuId
            this.dataForm.id = option.id || ''
			let _title = ""
			if(option.jurisdictionType =='btn_edit'){
				_title = "编辑"
			}
			if(option.jurisdictionType =='btn_detail'){
				_title = "详情"
			}
			if(option.jurisdictionType =='btn_add'){
				_title = "新增"
			}
            uni.setNavigationBarTitle({
                title: _title
            })
            this.selfInit()
            this.dataAll()
            this.initData()
        },
        onReady() {
            this.$refs.dataForm.setRules(this.rules);
        },
        watch:{
        	dataForm: {
				handler(val, oldVal) {
					this.mt_safe_archives_detail()
					this.mt_safe_7learnjindu()
					this.mt_safe_answersheet()
				},
				deep: true
			}
        },
        methods:{
        	checkChildRule() {
				let title = [];
				for (let k in this.ruleList) {
					let num = k.split("-");
					let childKey = num[0];
					num.forEach((model, i) => {
						if (i == 1) {
							let childData = this.dataForm[childKey]
							childData.forEach((child, i) => {
								if (child[model] instanceof Array) {
									if (child[model].length == 0) {
										title.push(this.ruleList[k])
									}
								} else {
									if (!child[model]) {
										title.push(this.ruleList[k])
									}
								}
							})
						}
					})
				}
				if (title.length > 0) {
					return title[0]
				}
			},
			resetForm(){
				uni.navigateBack()
			},
			dataAll(){
			    this.geteducationOptions()
			},
            geteducationOptions() {
                getDictionaryDataSelector('6a6d6fb541b742fbae7e8888528baa16').then(res => {
                    this.educationOptions = res.data.list
                })
            },
			mt_safe_archives_detail(){
				let table = this.dataForm.mt_safe_archives_detailList
				let summaryField =[]
				let summaryFieldName ={}
				let data ={}
				for (let i in summaryField) {
					let map = {}
					let val = 0
					for (let j = 0; j < table.length; j++) {
						let summary = table[j][summaryField[i]];
						if (summary) {
							let data = isNaN(summary) ? 0 :	Number(summary)
							val += data
						}
					}
					map.id = summaryField[i];
					map.name = summaryFieldName[summaryField[i]];
					map.val = val;
					data[summaryField[i]]=map;
				}
				return data;
			},
			mt_safe_7learnjindu(){
				let table = this.dataForm.mt_safe_7learnjinduList
				let summaryField =[]
				let summaryFieldName ={}
				let data ={}
				for (let i in summaryField) {
					let map = {}
					let val = 0
					for (let j = 0; j < table.length; j++) {
						let summary = table[j][summaryField[i]];
						if (summary) {
							let data = isNaN(summary) ? 0 :	Number(summary)
							val += data
						}
					}
					map.id = summaryField[i];
					map.name = summaryFieldName[summaryField[i]];
					map.val = val;
					data[summaryField[i]]=map;
				}
				return data;
			},
			mt_safe_answersheet(){
				let table = this.dataForm.mt_safe_answersheetList
				let summaryField =[]
				let summaryFieldName ={}
				let data ={}
				for (let i in summaryField) {
					let map = {}
					let val = 0
					for (let j = 0; j < table.length; j++) {
						let summary = table[j][summaryField[i]];
						if (summary) {
							let data = isNaN(summary) ? 0 :	Number(summary)
							val += data
						}
					}
					map.id = summaryField[i];
					map.name = summaryFieldName[summaryField[i]];
					map.val = val;
					data[summaryField[i]]=map;
				}
				return data;
			},
            initData(){
				this.$nextTick(function(){
					if (this.dataForm.id) {
						this.loading = true
						request({
							url: '/api/example/Mt_safe_archives/'+this.dataForm.id,
							method: 'get',
						}).then(res => {
							this.dataInfo(res.data)
							this.loading = false
						})
					}
				})
            },
            submitForm(){
                var _data = this.dataList()
                this.$refs.dataForm.validate(valid => {
                    if (!valid) return
                    if (!!this.checkChildRule()) return
                    this.$u.toast(
						this.checkChildRule()
					)
                    this.btnLoading = true
                    if (this.dataForm.id) {
                        request({
                            url: '/api/example/Mt_safe_archives/' + this.dataForm.id,
                            method: 'put',
                            data: _data,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        }).catch(() => {
							this.btnLoading = false
						})
                    }else {
                        request({
                            url: '/api/example/Mt_safe_archives/',
                            method: 'post',
                            data: _data,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        }).catch(() => {
							this.btnLoading = false
						})
                    }
                });
            },
            selfInit() {
				this.addmt_safe_archives_detailList()
				this.addmt_safe_7learnjinduList()
				this.addmt_safe_answersheetList()
                this.$store.commit('base/UPDATE_RELATION_DATA', {})
            },
            addmt_safe_archives_detailList(){
                let item = {
					mettingname:undefined,
					creatortime:undefined,
					score:undefined,
                }
				this.dataForm.mt_safe_archives_detailList.push(item)
            },
            delmt_safe_archives_detailList(index) {
                this.dataForm.mt_safe_archives_detailList.splice(index, 1);
            },
            addmt_safe_7learnjinduList(){
                let item = {
					classid:undefined,
					videodetailid:undefined,
					pace:undefined,
                }
				this.dataForm.mt_safe_7learnjinduList.push(item)
            },
            delmt_safe_7learnjinduList(index) {
                this.dataForm.mt_safe_7learnjinduList.splice(index, 1);
            },
            addmt_safe_answersheetList(){
                let item = {
					testpaper:undefined,
					creatortime:undefined,
					score:undefined,
                }
				this.dataForm.mt_safe_answersheetList.push(item)
            },
            delmt_safe_answersheetList(index) {
                this.dataForm.mt_safe_answersheetList.splice(index, 1);
            },
			dataList(){
				var _data = JSON.parse(JSON.stringify(this.dataForm));
				_data.companyname = JSON.stringify(_data.companyname)
				_data.lnchimage = JSON.stringify(_data.lnchimage)
				for(let i=0;i<_data.mt_safe_archives_detailList.length;i++){
				var _list = _data.mt_safe_archives_detailList[i];
				}
				for(let i=0;i<_data.mt_safe_7learnjinduList.length;i++){
				var _list = _data.mt_safe_7learnjinduList[i];
				}
				for(let i=0;i<_data.mt_safe_answersheetList.length;i++){
				var _list = _data.mt_safe_answersheetList[i];
				}
				return _data;
			},
			dataInfo(dataAll){
				let _dataAll =dataAll
				_dataAll.companyname = JSON.parse(_dataAll.companyname)
				_dataAll.lnchimage = JSON.parse(_dataAll.lnchimage)
				for(let i=0;i<_dataAll.mt_safe_archives_detailList.length;i++){
				var _list = _dataAll.mt_safe_archives_detailList[i];
				}
				for(let i=0;i<_dataAll.mt_safe_7learnjinduList.length;i++){
				var _list = _dataAll.mt_safe_7learnjinduList[i];
				}
				for(let i=0;i<_dataAll.mt_safe_answersheetList.length;i++){
				var _list = _dataAll.mt_safe_answersheetList[i];
				}
				this.dataForm=_dataAll
			},
        },
    }

</script>
